package com.pkk.spring.cloud.starter.rpc.mvc;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * Mvc的配置
 *
 * @author peikunkun
 * @version V1.0
 * @date 2021-01-07 17:46
 **/
public class MessageConverterOrderWebMvcConfigurer implements WebMvcConfigurer {


  @Autowired
  private MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter;


  /**
   * A hook for extending or modifying the list of converters after it has been
   * configured. This may be useful for example to allow default converters to
   * be registered and then insert a custom converter through this method.
   *
   * @param converters the list of configured converters to extend.
   * @since 4.1.3
   */
  @Override
  public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {

    //方法一:把jackson解析器放在第一位,这样匹配完了之后,就会直接返回;[是否匹配和我们解析器支持的类型有关[supportedMediaTypes]详细见源码
    // org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor#writeWithMessageConverters]
    //converters.removeIf(converter -> converter.getClass() == MappingJackson2HttpMessageConverter.class);
    converters.add(0, mappingJackson2HttpMessageConverter);

    //方法二:我们可以把匹配到的Spring自带的默认String值解析器去掉，这样匹配到的就有可能到jackson解析器来处理
    //converters.removeIf(converter -> converter.getClass() == StringHttpMessageConverter.class);
  }
}
